<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section [formGroup]="mqttIntegrationConfigForm">
  <section formGroupName="clientConfiguration">
    @if (!isNew) {
      <ng-container *ngTemplateOutlet="topicFilters"></ng-container>
    }
    <section class="pb-6" tb-hint-tooltip-icon="{{ 'integration.send-only-payload-hint' | translate }}">
      <mat-slide-toggle formControlName="sendOnlyMsgPayload">
        <span translate>integration.send-only-payload</span>
      </mat-slide-toggle>
    </section>
    <section class="flex gap-2">
      <mat-form-field class="flex-1" appearance="outline">
        <mat-label translate>admin.settings.host</mat-label>
        <input matInput formControlName="host" required>
        <tb-copy-button matSuffix
                        [class.!hidden]="!clientConfigurationFormGroup.get('host')?.value?.length"
                        [copyText]="clientConfigurationFormGroup.get('host').value">
        </tb-copy-button>
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'admin.settings.host-required' | translate }}"
                  [class.!hidden]="!clientConfigurationFormGroup.get('host').hasError('required')"
                  class="tb-error">
          warning
        </mat-icon>
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'integration.white-space-only' | translate }}"
                  [class.!hidden]="!clientConfigurationFormGroup.get('host').hasError('onlyWhitespace')"
                  class="tb-error">
          warning
        </mat-icon>
      </mat-form-field>
      <mat-form-field class="flex-1" appearance="outline">
        <mat-label translate>admin.settings.port</mat-label>
        <input matInput formControlName="port" type="number">
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'admin.settings.port-required' | translate }}"
                  [class.!hidden]="!clientConfigurationFormGroup.get('port').hasError('required')"
                  class="tb-error">
          warning
        </mat-icon>
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'admin.settings.port-pattern' | translate }}"
                  [class.!hidden]="!clientConfigurationFormGroup.get('port').hasError('pattern')"
                  class="tb-error">
          warning
        </mat-icon>
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'admin.settings.port-range' | translate }}"
                  [class.!hidden]="!(clientConfigurationFormGroup.get('port').hasError('min') || clientConfigurationFormGroup.get('port').hasError('max'))"
                  class="tb-error">
          warning
        </mat-icon>
      </mat-form-field>
    </section>
    <mat-expansion-panel class="expansion-panel-setting" [expanded]="clientConfigurationFormGroup.get('topicName').enabled">
      <mat-expansion-panel-header>
        <section tb-hint-tooltip-icon="{{ 'integration.topic-name-dynamic-hint' | translate }}">
          <mat-slide-toggle formControlName="useMsgTopicName" (click)="$event.stopPropagation()"
                            [aria-label]="'integration.topic-name-dynamic' | translate">
            <span translate>integration.topic-name-dynamic</span>
          </mat-slide-toggle>
        </section>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <mat-form-field class="mt-2 flex" appearance="outline">
          <mat-label translate>integration.topic-name</mat-label>
          <input matInput formControlName="topicName" [matAutocomplete]="auto">
          <mat-autocomplete #auto="matAutocomplete">
            @for (option of filteredTopics | async; track option) {
              <mat-option [value]="option">{{option}}</mat-option>
            }
          </mat-autocomplete>
          <tb-copy-button matSuffix
                          [class.!hidden]="!clientConfigurationFormGroup.get('topicName')?.value?.length"
                          [copyText]="clientConfigurationFormGroup.get('topicName').value">
          </tb-copy-button>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'integration.topic-name-required' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('topicName').hasError('required')"
                    class="tb-error">
            warning
          </mat-icon>
          <mat-icon matSuffix matTooltip="{{ 'integration.topic-name-hint' | translate }}">
            help
          </mat-icon>
        </mat-form-field>
      </ng-template>
    </mat-expansion-panel>
    <mat-form-field class="mat-block" appearance="outline">
      <mat-label translate>integration.client-id</mat-label>
      <input matInput formControlName="clientId">
      <tb-copy-button matSuffix
                      [class.!hidden]="!clientConfigurationFormGroup.get('clientId')?.value?.length"
                      [copyText]="clientConfigurationFormGroup.get('clientId').value">
      </tb-copy-button>
      <button mat-icon-button matSuffix
              [class.hidden]="!isNew && !isEdit()"
              [matTooltip]="'action.regenerate' | translate"
              matTooltipPosition="above"
              (click)="generateClientId()">
        <mat-icon>
          autorenew
        </mat-icon>
      </button>
      <mat-icon matSuffix matTooltip="{{ 'integration.client-id-hint' | translate }}">
        help
      </mat-icon>
      <mat-icon matSuffix
                matTooltipClass="tb-error-tooltip"
                matTooltip="{{ 'integration.client-id-required' | translate }}"
                [class.!hidden]="!clientConfigurationFormGroup.get('clientId').hasError('required')"
                class="tb-error">
        warning
      </mat-icon>
    </mat-form-field>
    <section class="mb-4">
      <tb-integration-credentials formControlName="credentials"
                                  [passwordOptional]="true"
                                  [userNameOptional]="true"
                                  [allowCredentialTypes]="[IntegrationCredentialType.Anonymous,
                                                         IntegrationCredentialType.Basic,
                                                         IntegrationCredentialType.CertPEM]">
        @if (displayEnableSsl()) {
          <mat-slide-toggle formControlName="ssl">
            {{ 'integration.enable-ssl' | translate }}
          </mat-slide-toggle>
        }
      </tb-integration-credentials>
    </section>
    <section class="flex items-center gap-2" [class.!hidden]="disabled || isNew">
      <ng-container [ngTemplateOutlet]="executeRemotelyTemplate()">
      </ng-container>
      <mat-icon class="tb-mat-20 mb-2" style="color: rgba(0,0,0,0.18)"
                [matTooltip]="'integration.check-connection-mqtt-hint' | translate"
                matTooltipPosition="right">
        info</mat-icon>
    </section>
    <mat-expansion-panel class="configuration-panel">
      <mat-expansion-panel-header>
        <mat-panel-description class="flex items-stretch justify-end" translate>
          integration.advanced-settings
        </mat-panel-description>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.keep-alive-sec</mat-label>
          <input matInput formControlName="keepAliveSec" type="number" min="0" max="65535">
          <mat-icon matSuffix matTooltip="{{ 'mqtt-client-session.keep-alive-hint' | translate }}">
            help
          </mat-icon>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'mqtt-client-session.keep-alive-required' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('keepAliveSec').hasError('required')"
                    class="tb-error">
            warning
          </mat-icon>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'event.min-value' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('keepAliveSec').hasError('min')"
                    class="tb-error">
            warning
          </mat-icon>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'mqtt-client-session.keep-alive-max' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('keepAliveSec').hasError('max')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.connect-timeout-sec</mat-label>
          <input matInput formControlName="connectTimeoutSec" type="number" min="0">
          <mat-icon matSuffix matTooltip="{{ 'ws-client.connections.connect-timeout-hint' | translate }}">
            help
          </mat-icon>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'ws-client.connections.connect-timeout-required' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('connectTimeoutSec').hasError('required')"
                    class="tb-error">
            warning
          </mat-icon>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'event.min-value' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('connectTimeoutSec').hasError('min')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.reconnect-period-sec</mat-label>
          <input matInput formControlName="reconnectPeriodSec" type="number" min="0">
          <mat-icon matSuffix matTooltip="{{ 'ws-client.connections.reconnect-period-hint' | translate }}">
            help
          </mat-icon>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'ws-client.connections.reconnect-period-required' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('reconnectPeriodSec').hasError('required')"
                    class="tb-error">
            warning
          </mat-icon>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'event.min-value' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('reconnectPeriodSec').hasError('min')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>ws-client.connections.mqtt-version</mat-label>
          <mat-select formControlName="mqttVersion">
            @for (mqttVersion of mqttVersions; track mqttVersion) {
              <mat-option [value]="mqttVersion.value">
                {{ mqttVersion.name }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
        <mat-expansion-panel class="expansion-panel-setting" [expanded]="clientConfigurationFormGroup.get('qos').enabled">
          <mat-expansion-panel-header>
            <section tb-hint-tooltip-icon="{{ 'integration.mqtt-qos-dynamic-hint' | translate }}">
              <mat-slide-toggle formControlName="useMsgQoS" (click)="$event.stopPropagation()"
                                [aria-label]="'integration.mqtt-qos-dynamic' | translate">
                <span translate>integration.mqtt-qos-dynamic</span>
              </mat-slide-toggle>
            </section>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <tb-qos-select formControlName="qos" class="mt-2 flex" appearance="outline">
            </tb-qos-select>
          </ng-template>
        </mat-expansion-panel>
        <mat-expansion-panel class="expansion-panel-setting" [expanded]="clientConfigurationFormGroup.get('retained').enabled">
          <mat-expansion-panel-header>
            <section tb-hint-tooltip-icon="{{ 'integration.retain-dynamic-hint' | translate }}">
              <mat-slide-toggle formControlName="useMsgRetain" (click)="$event.stopPropagation()"
                                [aria-label]="'integration.retain-dynamic' | translate">
                <span translate>integration.retain-dynamic</span>
              </mat-slide-toggle>
            </section>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <mat-slide-toggle class="pb-4" formControlName="retained">
              {{ 'integration.retain' | translate }}
            </mat-slide-toggle>
          </ng-template>
        </mat-expansion-panel>
        <ng-container [ngTemplateOutlet]="genericAdditionalInfoTemplate()">
        </ng-container>
      </ng-template>
    </mat-expansion-panel>
  </section>
  <ng-template #topicFilters>
    <section class="mb-6" [formGroup]="mqttIntegrationConfigForm">
      <section class="security-setting mat-padding">
        <p translate>integration.topic-filters</p>
        <tb-integration-topic-filters [isEdit]="isEdit()" [integration]="integration()" formControlName="topicFilters">
        </tb-integration-topic-filters>
      </section>
    </section>
  </ng-template>
</section>
